<link rel="stylesheet" type="text/css" href="/css/admin/stylesheet.css" />
<link rel="stylesheet" type="text/css" href="/css/admin/editor.css" />
<link rel="stylesheet" type="text/css" href="/js/admin/jquery/ui/themes/ui-lightness/jquery-ui-1.8.16.custom.css" />
<script src="/js/admin/jquery/jquery-1.6.1.min.js" type="text/javascript"></script>
<script src="/js/admin/jquery/ui/jquery-ui-1.8.16.custom.min.js" type="text/javascript"></script>
<script src="/js/admin/jquery/ui/external/jquery.bgiframe-2.1.2.js" type="text/javascript"></script>
<script src="/js/admin/jquery/tabs.js" type="text/javascript"></script>
<script src="/js/admin/jquery/superfish/js/superfish.js" type="text/javascript"></script>
<link rel="stylesheet" href="/css/prettyPhoto.css" type="text/css" media="screen" title="prettyPhoto main stylesheet" charset="utf-8" />
<script src="/js/jquery.prettyPhoto.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
	$(document).ready(function(){
		$("area[rel^='prettyPhoto']").prettyPhoto({autoplay_slideshow: false,slideshow: false,deeplinking: false});
		$(".gallery:first a[rel^='prettyPhoto']").prettyPhoto({slideshow:false, autoplay_slideshow: false,deeplinking: false});
		$(".gallery:gt(0) a[rel^='prettyPhoto']").prettyPhoto({slideshow:false, autoplay_slideshow: false, hideflash: true,deeplinking: false});
	});
</script>


<script type="text/javascript" src="/js/cbrte/html2xhtml.min.js"></script>
<script type="text/javascript" src="/js/cbrte/richtext_compressed.js"></script>
<script type="text/javascript" src="/js/cbrte/init.js"></script>
<script type="text/javascript" src="/js/jquery-swapsies.js"></script>
<link rel="stylesheet" type="text/css" href="/js/admin/jquery/ui/themes/ui-lightness/jquery.ui.all.css" />

<?php  if(isset($this->success)) {?>
<div class="success" style="text-align: center;"><?php echo $this->success;?></div>
<?php }?>
<?php if(isset($this->warning)) {?>
<div class="warning" style="text-align: center;"><?php  echo $this->warning;?></div>
<?php }?>
<?php if (isset($_SESSION['success'])&&($_SESSION['success'] == 1)){?>
<?php unset($_SESSION['success']);?>
	<script type="text/javascript">
						  window.opener.location.href = window.opener.location.href;
						  if (window.opener.progressWindow)
						  {
						    window.opener.progressWindow.close();
						  }
						  window.close();
					</script>
<?php }?>


<div class="box">
 <div class="heading">
      <h1><img alt="" src="/images/admin/setting.png"><?php echo $this->language;?></h1>
      <div class="buttons">
   
	 
	 
	      <a class="button" onclick="done_editor();"><span>Done</span></a>
	      <a class="button" onclick="preview_editor();"><span>Preview</span></a>
	      <a class="button" onclick="reset();"><span>Reset</span></a>
      </div>
    </div>
    <div class="content">
    	<form id="block" enctype="multipart/form-data" method="get" action="/admin/editor">
	    	<?php $i=0; foreach($this->block as $block){ $i++;?>
	    		<?php if(!isset($block['deleted'])){?>
		    	<div id="block<?php echo $i;?>" class="block">
		    		<div style="float:right;">	
			    		<a onclick="move_up(<?php echo $i;?>);"><img src="/images/admin/Arrow-Up.png"/></a><br/>
						<a onclick="move_down(<?php echo $i;?>);"><img src="/images/admin/Arrow-Down.png"/></a><br/><br/>
						<a onclick="delete_block(<?php echo $i;?>);"><img src="/images/admin/remove.png"/></a>
					</div>
					<?php if($block['type'] == 1) {?>
					<div class="gallery clearfix">
							<div class="image_block">
								<?php if($block['image'] != "") {?>
									<?php $j=0; foreach($block['image'] as $image){ $j++;?>
									<a href="/admin/editor/editorimage?i=<?php echo $i;?>&j=<?php echo $j;?>&<?php echo str_replace(array("\r\n", "\n", "\r"), '', trim(str_replace('\\', '', http_build_query(array('data' => $image)))));?>&iframe=true&width=700&height=420" rel="prettyPhoto[iframe]" style="text-decoration: none;color:black;">
										<?php echo Util::image_generator($image);?>
									</a>
									<?php }?>
								<?php }?>
								<a href="/admin/editor/editortext?i=<?php echo $i;?>&data=<?php echo urlencode(str_replace(array("\r\n", "\n", "\r"), '', trim(str_replace('\\', '', $block['data']))));?>&iframe=true&width=700&height=300" rel="prettyPhoto[iframe]" style="text-decoration: none;color:black;">
									<?php echo str_replace('\\', '', $block['data']);?>
								</a>
								<div style="clear: both;"></div>
							</div>
							
						<div style="background-color: silver;width:96%;height:30px;">
							<a style="float:right;margin-right:5px;" class="button" onclick="add_image_block(<?php echo $i;?>);"><span>Add Image Block</span></a>
						</div>
					</div>
					<?php }?>
					
					<?php if($block['type'] == 3) {?>
					 <div class="gallery clearfix">
						 <a href="/admin/editor/editortable?i=<?php echo $i;?>&data=<?php echo urlencode(str_replace(array("\r\n", "\n", "\r"), '', trim(str_replace('\\', '', $block['data']))));?>&iframe=true&width=700&height=300" rel="prettyPhoto[iframe]" style="text-decoration: none;color:black;">			
							<div class="image_block">
								<?php echo $block['data'];?>
						  	</div>
					  	</a>
					 </div>
					<?php }?>
					
					<?php if($block['type'] == 4) {?>
					  <div class="image_block" style="text-align: center;font-weight: bold;">
					  	<br/>Break Block<br/><br/>
						<?php echo $block['data'];?>
						<br/>Break Block
					  </div>
					<?php }?>
					
					<input id="sort<?php echo $i;?>" type="hidden" name="sort<?php echo $i;?>" value="<?php echo $i;?>" />			
				</div>
				<?php }?>
	    	<?php }?>
	    	<input id="type" type="hidden" name="type" value="0" />
	    	<input id="image" type="hidden" name="image" value="0" />
	    	<input id="table" type="hidden" name="table" value="" />
    	</form>
    	<br/>
    	<div style="text-align: center;">
    		<a class="button" onclick="add_text_block();"><span>Add Text Block</span></a>
    		<a class="button" onclick="add_table_block();"><span>Add Table Block</span></a>
    		<a class="button" onclick="add_break_block();"><span>Add Break Block</span></a>
    	</div>
		
		<form id="form" enctype="multipart/form-data" method="post" action="/admin/editor">

         </form>
    </div>
  </div>
<?php //$session = new Zend_Session_Namespace('block.auth');
		//echo print_r($session->block);echo print_r("aa".sizeof($session->block));
		//echo print_r("<br/>");
		//echo print_r($this->block); ?>
  <script type="text/javascript">
   function add_text_block(){
	   updateRTEs();
	   $('#type').val(1);
	   $('#block').submit();
   }

   function add_image_block(i){
	   updateRTEs();
	   $('#type').val(2);
	   $('#image').val(i);
	   $('#block').submit();
   }

   function add_table_block(){
	   var left = (screen.width/2)-(380/2);
	   var top = (screen.height/2)-(250/2);
	   window.open("/js/cbrte/insert_table.htm", "Insert Table", "width=380,height=250,scrollbars=yes,top="+top+", left="+left);
   }

   function add_break_block(){
	   updateRTEs();
	   $('#type').val(4);
	   $('#block').submit();
   }
   
   function do_it(data){
	   updateRTEs();
	   $('#table').val(data);
	   $('#type').val(3);
	   $('#block').submit();
   }
   
   function reset(){
	   <?php if(isset($_SESSION['edit_thing'])){?>
	   	document.location.href = "/admin/editor?reset=1&id=<?php echo $_SESSION['edit_thing'];?>&lang=<?php echo $_SESSION['lang'];?>";
	   <?php } elseif (isset($_SESSION['edit_page'])){?>
	  	 document.location.href = "/admin/editor?reset=1&page=<?php echo $_SESSION['edit_page'];?>&lang=<?php echo $_SESSION['lang'];?>";
	   <?php }?>
   }

   function done_editor(){
	   updateRTEs();
	   $('#type').val(-2);
	   $('#block').submit();
   }

   function preview_editor(){
	   updateRTEs();
	   $('#type').val(-1);
	   $('#block').submit();
   }
   
   function move_up(pos){
	   var cur = $('#sort'+pos).val();
	   var next_o = 0;
	   var next = 0;
	   for(i=1;i<=<?php echo sizeof($this->block)?>;i++)
	   {
		   if($('#sort'+i).val()<$('#sort'+pos).val() && $('#sort'+i).val() >= next_o)
		   {
			   next = i;
			   next_o = $('#sort'+i).val();
		   }
	   }
	   if(next==0) return false;

	   $("#block"+pos).fadeOut('fast', function() {
		   $("#block"+next).fadeOut('fast', function() {
			   $('#sort'+pos).val($('#sort'+next).val());
			   $('#sort'+next).val(cur);

			   var i1 = pos ;
			   var i2 = next ;
			   var sort1 =  $('#sort'+pos).val();
			   var sort2 =  $('#sort'+next).val();
			   
				if (window.XMLHttpRequest)
				{// code for IE7+, Firefox, Chrome, Opera, Safari
					xmlhttp=new XMLHttpRequest();
				}
				else
				{// code for IE6, IE5
					xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
				}
				xmlhttp.onreadystatechange = function()
				{	
					if (xmlhttp.readyState==4 && xmlhttp.status==200)
					{
						
					}	
				};
				var url = "/admin/editor/sortblock?i1="+i1+"&i2="+i2+"&sort1="+sort1+"&sort2="+sort2;
				xmlhttp.open("GET",url,true);	
				xmlhttp.send();
			   
			   var $block = $('#block'+next);
			   var $pivot = $block.next();
			   var $blockToSwap = $pivot.next();
			   $blockToSwap.before($pivot);
			   $block.insertAfter($pivot);
			   $block.attr('style','');
			   $pivot.attr('style','');
			   });
		   });
	   
	   $("#block"+pos).fadeIn('fast', function() {
		   $("#block"+next).fadeIn();
		   });
   }

   function move_down(pos){
	   var cur = $('#sort'+pos).val();
	   var next = <?php echo sizeof($this->block)+1?>;
	   var next_o = <?php echo sizeof($this->block)+1?>;
	   for(i=1;i<=<?php echo sizeof($this->block)?>;i++)
	   {
		   if($('#sort'+i).val()>$('#sort'+pos).val() && $('#sort'+i).val() <= next_o)
		   {
			   next = i;
			   next_o = $('#sort'+i).val();
		   }
	   }
	   if(next==<?php echo sizeof($this->block)+1?>) return false;
	   $("#block"+pos).fadeOut('fast', function() {
		   $("#block"+next).fadeOut('fast', function() {
			   $('#sort'+pos).val($('#sort'+next).val());
			   $('#sort'+next).val(cur);

			   var i1 = pos ;
			   var i2 = next ;
			   var sort1 =  $('#sort'+pos).val();
			   var sort2 =  $('#sort'+next).val();
			   
				if (window.XMLHttpRequest)
				{// code for IE7+, Firefox, Chrome, Opera, Safari
					xmlhttp=new XMLHttpRequest();
				}
				else
				{// code for IE6, IE5
					xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
				}
				xmlhttp.onreadystatechange = function()
				{	
					if (xmlhttp.readyState==4 && xmlhttp.status==200)
					{
						
					}	
				};
				var url = "/admin/editor/sortblock?i1="+i1+"&i2="+i2+"&sort1="+sort1+"&sort2="+sort2;
				xmlhttp.open("GET",url,true);	
				xmlhttp.send();
				
			   var $block = $('#block'+pos);
			   var $pivot = $block.next();
			   var $blockToSwap = $pivot.next();
			   $blockToSwap.before($pivot);
			   $block.insertAfter($pivot);
			   $block.attr('style','');
			   $pivot.attr('style','');
			   });
		   });
	   
	   $("#block"+pos).fadeIn('fast', function() {
		   $("#block"+next).fadeIn();
		   });
   }

   function delete_block(pos){
	   if(!window.confirm('Bạn có chắc muốn xóa block này không ?'))
		   return false;
	   if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
			    $("#block"+pos).animate({ height: 0,  opacity: 0 }, 250 ,function()
			      {
				    	$("#block"+pos).remove() ; 
			      }
			     ); 
			}
		}
		xmlhttp.open("GET","/admin/editor/deleteblock?id="+pos,true);
		xmlhttp.send();
   }
  </script>
  

<script type="text/javascript"><!--
function image_upload(field, preview, id) {
	$('#dialog').remove();
	
	$('#content').prepend('<div id="dialog" style="padding: 3px 0px 0px 0px;"><iframe src="/admin/filemanager?field=' + encodeURIComponent(field) + '" style="padding:0; margin: 0; display: block; width: 100%; height: 100%;" frameborder="no" scrolling="auto"></iframe></div>');
	
	$('#dialog').dialog({
		title: 'Images Manager',
		close: function (event, ui) {
			if ($('#' + field).attr('value')) {
				$('#' + preview).replaceWith('<img src="' +$('#' + field).attr('value') + '" alt="" id="' + preview + '" class="image" onclick="image_upload(\'' + field + '\', \'' + preview + '\', \'' + id + '\');" />');

				var pic_real_width, pic_real_height;
				$("<img/>") // Make in memory copy of image to avoid css issues
				    .attr("src", $('#' + field).attr('value'))
				    .load(function() {
				        pic_real_width = this.width;   // Note: $(this).width() will not
				        pic_real_height = this.height; // work for in memory images.
				        $('#width' + id).val(pic_real_width);
						$('#height' + id).val(pic_real_height);
				    });
			}
		},	
		bgiframe: false,
		width: 700,
		height: 400,
		resizable: false,
		modal: false
	});
};
//--></script>

<script type="text/javascript">
			<!--
			function numbersonly(myfield, e)
			{
			var key;
			var keychar;
			if (window.event)
			key = window.event.keyCode;
			else if (e)
			key = e.which;
			else
			return true;
			keychar = String.fromCharCode(key);
			// control keys
			if ((key==null) || (key==0) || (key==8) ||
			(key==9) || (key==27) || (key==13))
			return true;
			// numbers
			else if ((("0123456789").indexOf(keychar) > -1))
			return true;
			// decimal point jump
			else
			return false;
			}
			//-->
</script>  